package com.gjy.basic;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-03-26 15:20:16
 */
public class StringUtils {

    public static Set<String> splitToSet(String str, String delimiter) {
        if (str == null || str.isEmpty()) {
            return Collections.emptySet();
        }
        Set<String> set = new HashSet<>();
        for (String item : str.split(delimiter)) {
            if (!item.trim().isEmpty()) {
                set.add(item.trim());
            }
        }
        return Collections.unmodifiableSet(set);
    }

    private static final Set<String> GROUP_FIELDS_SET = StringUtils.splitToSet("GROUP_FIELDS", ",");

    public static boolean containsGroupField(String columnName) {
        return columnName != null && GROUP_FIELDS_SET.contains(columnName.trim());
    }

    public static boolean validateFieldsExist(String input, Set<String> requiredFields, Set<String> excludedFields) {
        if (input == null || input.isEmpty()) {
            return requiredFields.isEmpty();
        }

        String requiredPattern = requiredFields.stream()
                .map(f -> "\\b" + Pattern.quote(f) + "\\b")
                .collect(Collectors.joining("|", "(?=.*(?:", "))"));

        String excludedPattern = excludedFields.stream()
                .map(f -> "\\b" + Pattern.quote(f) + "\\b")
                .collect(Collectors.joining("|", "(?!.*(?:", "))"));

        Pattern pattern = Pattern.compile("^" + requiredPattern + excludedPattern + ".*$", Pattern.CASE_INSENSITIVE);
        return pattern.matcher(input).matches();
    }

}
